package com.ruoyi.web.controller.core;

import java.util.List;
import java.util.Optional;

import com.ruoyi.core.domain.Codes;
import com.ruoyi.core.domain.Product;
import com.ruoyi.core.domain.vo.GiftCardVo;
import com.ruoyi.core.service.ICodesService;
import com.ruoyi.core.service.IProductService;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.GiftCard;
import com.ruoyi.core.service.IGiftCardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 礼品卡Controller
 * 
 * @author lgc
 * @date 2020-08-08
 */
@Controller
@RequestMapping("/core/card")
public class GiftCardController extends BaseController
{
    private String prefix = "core/card";

    @Autowired
    private IGiftCardService giftCardService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ICodesService codeService;

    @RequiresPermissions("core:card:view")
    @GetMapping()
    public String card(Model model)
    {
        List<Product> products = productService.selectProductAll(ShiroUtils.getUserId());
        model.addAttribute("products",products);
        return prefix + "/card";
    }

    /**
     * 检测该序号的礼品卡是否存在
     */
    @PostMapping( "/getRemoveCode")
    @ResponseBody
    public Boolean  getRemoveCode(Long removeCode){
        Codes codes = codeService.selectCodesBy(removeCode,ShiroUtils.getUserId());
        //没有相应的码
        if ( !Optional.ofNullable(codes).isPresent()){
            return false;
        }
        //是否存在
        Boolean flag = !giftCardService.checkCode(codes.getCode(),ShiroUtils.getUserId());
        return flag;
    }

    /**
     * 检测序号是否重复
     */
    @PostMapping( "/getCode")
    @ResponseBody
    public Boolean  getStartCode(Long index){
        Codes codes = codeService.selectCodesBy(index,ShiroUtils.getUserId());
        //该对象不存在返回false
        if ( !Optional.ofNullable(codes).isPresent()){
            return false;
        }
        //是否存在
        Boolean flag = giftCardService.checkCode(codes.getCode(),ShiroUtils.getUserId());
        return flag;
    }


    /**
     * 查询礼品卡列表
     */
    @RequiresPermissions("core:card:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GiftCard giftCard)
    {
        giftCard.setCreatedBy(ShiroUtils.getUserId());
        startPage();
        List<GiftCardVo> list = giftCardService.selectGiftCardList(giftCard);
        return getDataTable(list);
    }

    /**
     * 导出礼品卡列表
     */
    @RequiresPermissions("core:card:export")
    @Log(title = "礼品卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GiftCard giftCard)
    {
        List<GiftCardVo> list = giftCardService.selectGiftCardList(giftCard);
        ExcelUtil<GiftCardVo> util = new ExcelUtil<GiftCardVo>(GiftCardVo.class);
        return util.exportExcel(list, "card");
    }

    /**
     * 新增礼品卡
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Product> products = productService.selectProductAll(ShiroUtils.getUserId());
        model.addAttribute("products",products);
        return prefix + "/add";
    }
    /**
     * 批量剔除
     */
    @GetMapping("/removeCodes")
    public String removeCode()
    {
        return prefix + "/remoCode";
    }

    /**
     * 剔除礼品卡
     */
    @RequiresPermissions("core:card:remove")
    @Log(title = "礼品卡", businessType = BusinessType.DELETE)
    @PostMapping("/removeCodes")
    @ResponseBody
    public AjaxResult removeCodes(Long startCode, Long endCode)
    {
        if (endCode<startCode){
            return error("结束序列号应大于开始序号");
        }
        return toAjax(giftCardService.updateStatesBy(startCode,endCode,ShiroUtils.getUserId()));
    }


    /**
     * 新增保存礼品卡
     */
    @RequiresPermissions("core:card:add")
    @Log(title = "礼品卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long startCode, Long endCode,GiftCard giftCard)
    {
        if (endCode<startCode){
            return error("结束序列号应大于开始序号");
        }
        return toAjax(giftCardService.insertGiftCard(giftCard,startCode,endCode,ShiroUtils.getUserId()));
    }

    /**
     * 修改礼品卡
     */
    @GetMapping("/edit/{giftCardId}")
    public String edit(@PathVariable("giftCardId") Long giftCardId, ModelMap mmap)
    {
        GiftCard giftCard = giftCardService.selectGiftCardById(giftCardId);
        mmap.put("giftCard", giftCard);
        return prefix + "/edit";
    }

    /**
     * 修改保存礼品卡
     */
    @RequiresPermissions("core:card:edit")
    @Log(title = "礼品卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GiftCard giftCard)
    {
        return toAjax(giftCardService.updateGiftCard(giftCard));
    }

    /**
     * 删除礼品卡
     */
    @RequiresPermissions("core:card:remove")
    @Log(title = "礼品卡", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(giftCardService.updateStatesByIds(ids,ShiroUtils.getUserId()));
    }
}
